package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 分页查询
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}",queryPageBean);
        PageResult pageResult=setmealService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询:{}",id);
        Setmeal setmeal =setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkGroupIds,@RequestBody Setmeal setmeal){
        log.info("编辑检套餐{}");
        setmealService.edit(checkGroupIds,setmeal);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 添加套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkgroupIds,@RequestBody Setmeal setmeal){
        log.info("添加套餐{}",setmeal);
        setmealService.add(checkgroupIds,setmeal);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 根据id删除套餐
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        log.info("删除套餐{}",id);
        setmealService.deleteById(id);
        return new Result(true,"删除成功");
    }
    /**
     * 上传文件
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        log.info("上传文件中...");
        //获的原始文件的名字
        String originalFilename = imgFile.getOriginalFilename();
        //截取原始文件名-获的扩展名
        String exctension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用uuid构造新的文件名
        String newFileName = UUID.randomUUID()+ exctension;
        //使用阿里云工具类上传文件
        try {
            String filePath = aliOssUtil.upload(imgFile.getBytes(), newFileName);
            log.info("文件上传成功:{}",filePath);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error("文件上传失败{}",e.getMessage());
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
    }
}